package leetcode.code0529;

import leetcode.IDebug;

public class Solution implements IDebug {

	// 当前位置周围没有雷的时候才可以向周围未访问过的E节点进行标记
	int[] direction = new int[] { -1, -1, -1, 0, -1, 1, 0, -1, 0, 1, 1, -1, 1, 0, 1, 1 };
	int m, n;

	public char[][] updateBoard(char[][] board, int[] click) {
		int x = click[0], y = click[1];
		if (board[x][y] == 'M') {
			board[x][y] = 'X';
			return board;
		}
		m = board.length;
		n = board[0].length;
		update(x, y, board);
		return board;
	}

	private void update(int x, int y, char[][] array) {
		int cnt = 0;
		for (int k = 0; k < direction.length; k += 2) {
			int nextx = x + direction[k], nexty = y + direction[k + 1];
			if (nextx >= 0 && nextx < m && nexty >= 0 && nexty < n) {
				if (array[nextx][nexty] == 'M') {
					cnt++;
				}
			}
		}
		if (cnt == 0) {
			array[x][y] = 'B';
			for (int k = 0; k < direction.length; k += 2) {
				int nextx = x + direction[k], nexty = y + direction[k + 1];
				if (nextx >= 0 && nextx < m && nexty >= 0 && nexty < n) {
					if (array[nextx][nexty] == 'E') {
						this.update(nextx, nexty, array);
					}
				}
			}
		} else {
			array[x][y] = (char) ('0' + cnt);
		}
	}

	@Override
	public void debug4() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug3() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug2() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug1() {
		// TODO Auto-generated method stub

	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub

	}

}
